<template>
  <div class="login-container">
    <el-card class="login-form-layout">
      <el-form autoComplete="on"
               :model="loginForm"
               :rules="loginRules"
               ref="loginForm"
               label-position="left">
        <div style="text-align: center;">
          <img :src="face" style="width: 100px;" />
        </div>
        <h2 class="login-title color-main">商品后台管理系统</h2>

        <el-form-item prop="username">
          <el-input name="username"
                    type="text"
                    v-model="loginForm.username"
                    autoComplete="on"
                    placeholder="请输入用户名">
            <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input name="password"
                    :type="pwdType"
                    v-model="loginForm.password"
                    autoComplete="on"
                    placeholder="请输入密码">
            <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span>
            <span slot="suffix" @click="showPwd">
              <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>

        <el-form-item prop="inputImageCode">
          <el-col :span="8">
            <img id="img" alt="验证码" @click="retrieveKaptcha()" :src="path" />
          </el-col>
          <el-col :span="16" style="margin-top: 5px;">
            <el-input v-model="loginForm.inputImageCode"
                      type="text"
                      auto-complete="off"
                      @keyup.enter.native="checkCode"
                      placeholder="请输入验证码">
            </el-input>
          </el-col>
        </el-form-item>

        <el-form-item style="margin-bottom: 60px;text-align: center">
          <el-button style="width: 100%;" type="primary" :loading="loading" @click.native.prevent="checkCode">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <img :src="loginBG" class="login-center-layout">
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate';
import { setSupport, getSupport, setCookie, getCookie } from '@/utils/support';
import face from '@/assets/images/face.jpg'
import loginBG from '@/assets/images/login_bg.jpg'

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能小于3位'))
      } else {
        callback()
      }
    };
    const validateCode = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('验证码不能为空，请输入后登录'))
      } else {
        callback()
      }
    };
    return {
      loginForm: {
        username: '',
        password: '',
        inputImageCode: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePass}],
        inputImageCode: [{required: true, trigger: 'blur', validator: validateCode}]
      },
      loading: false,
      pwdType: 'password',
      face,
      loginBG,
      path: this.$baseURL + 'KaptchaController/createImageCode'
    }
  },
  created() {
    this.loginForm.username = getCookie("username")
    this.loginForm.password = getCookie("password")
    if (this.loginForm.username === undefined || this.loginForm.username == null || this.loginForm.username === '') {
      this.loginForm.username = 'admin'
    }
    if (this.loginForm.password === undefined || this.loginForm.password == null) {
      this.loginForm.password = ''
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    retrieveKaptcha() {
      this.path = this.$baseURL + 'KaptchaController/createImageCode?d=' + new Date() * 1
    },
    checkCode() {
      if (this.loginForm.inputImageCode.length < 1) {
        this.$message.error('验证码不能为空，请输入后登录');
        return;
      }

      this.$ajax({
        url: '/KaptchaController/checkCode',
        method: 'post',
        data: {
          code: this.loginForm.inputImageCode
        }
      }).then(response => {

        if (response.code == '200') {

          this.handleLogin();

        }

      }).catch(error => {
        this.retrieveKaptcha();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false;
            setCookie("username", this.loginForm.username, 15);
            setCookie("password", this.loginForm.password, 15);
            this.$router.push({path: '/'})
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('参数验证不合法！');
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.el-input__inner {
  background-color: transparent;
}

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
  -webkit-box-shadow: 0 0 0 68px transparent inset;
  transition: background-color 5000s ease-in-out 0s !important;
}
</style>

<style scoped lang="scss">
.login-form-layout {
  width: 600px;
  margin: 12% auto 0 auto;
  z-index: 1;
  position: relative;
  overflow: hidden;
  border-radius: 10px;
}

.login-form-layout::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
  background: url("../../assets/images/login_bg.jpg") no-repeat fixed;
  background-size: cover;
  filter: blur(20px);
  margin: -30px;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 100%;
  min-width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
}
</style>
